/*
 * Copyright (c) 2004-2006 The Regents of The University of Michigan
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Gou Pengfei
 * Date:    April. 2011
 */

#ifndef __CPU_EDGE_LOCAL_EXIT_HH__
#define __CPU_EDGE_LOCAL_EXIT_HH__

#include <vector>
#include <deque>

#include "cpu/edge/pred/base_exit_predictor.hh"
#include "cpu/edge/sat_counter.hh"

template<class Impl>
class LocalExitP : public BaseExitPredictor<Impl>
{
  public:
    typedef TheISA::BlockID BlockID;
    typedef TheISA::ExitID ExitID;

    enum TableMode {
        Normal,
        AliasFree,
        InvalidTableMode
    };

    enum HysteresisMode {
        Symmetric,
        Asymmetric,
        InvalidHysteresisMode
    };

    LocalExitP(DerivEdgeCPUParams *params);

    ~LocalExitP();

    void reset();	
    /**
     * Looks up the given block address in the branch predictor and returns
     * an exit ID of the block. 
     */
    TheISA::ExitID lookup(BlockID block_id, BlockID oldest_block_id, 
        Addr &block_addr, int addr_space_ID, ThreadID tid);   

    /**
     * Updates the branch predictor with the actual result of a branch.
     * Return the prediction before update.
     */
    TheISA::ExitID update(BlockID block_id, Addr &block_addr, int addr_space_id,
                              ExitID actual_exit_id,ThreadID tid);

    /**
     * Restores the global branch history on a squash.
     */
    void squash(BlockID block_id, Addr block_pc, ExitID exit_id,
            ThreadID tid);

    /**
     * Restores the global branch history on a squash.
     */
    void squash(BlockID block_id, ThreadID tid);

    int getCounterBits() { return localCtrBits; }

  private:

    struct PredictionTableEntry {
        PredictionTableEntry(BasePredictor<Impl> * ptr)
            : predictorPtr(ptr),
            exitID(0),
            hysteresis(ptr->getCounterBits())
        {}

	~PredictionTableEntry()
	{}

        BasePredictor<Impl> * predictorPtr;

	ExitID exitID;
        SatCounter hysteresis;
    };

    struct HistoryTableEntry {
        HistoryTableEntry(BasePredictor<Impl> * ptr)
            : predictorPtr(ptr),
            hist(0x0)
        {}

        ~HistoryTableEntry()
        {}

        BasePredictor<Impl> * predictorPtr;

        uint64_t hist;
    };

    struct LFFEntry {
        uint64_t index;
	uint64_t history;
    };

    void initVars();

    TableMode tableMode;

    HysteresisMode hysteresisMode;

    inline uint64_t calcLocHistIdx(Addr &block_addr, int addr_space_ID);

    inline uint64_t calcLocPredIdx(Addr &block_addr, uint64_t history);

    void updateHysteresis(PredictionTableEntry &entry,
            ExitID actual_exit_id);

    /** Number of bits for local hysteresis. */
    int localCtrBits;
	
    /** Local counters. */
    PredictorTable<PredictionTableEntry, Impl> localCtrs;

    /** Size of the local predictor. */
    uint64_t localPredictorSize;

    /** Mask to get the proper index bits into the predictor. */
    uint64_t localPredictorMask;

    /** Array of local history table entries. */
    PredictorTable<HistoryTableEntry, Impl> localHistoryTable;

    /** Array of local future file. */
    std::vector<LFFEntry> localFutureFile;
	
    /** Size of the local history table. */
    uint64_t localHistoryTableSize;

    /** Mask of the local history table. */
    uint64_t localHistoryTableMask;

    /** Number of bits for each entry of the local history table.
     *  @todo Doesn't this come from the size of the local predictor?
     */
    uint64_t localHistoryBits;

    /** Mask to get the proper local history. */
    uint64_t localHistoryMask;

    /** Number of bits to shift the instruction over to get rid of the word
     *  offset.
     */
    uint64_t blockShiftAmt;

    /** Threshold for the counter value; above the threshold is taken,
     *  equal to or below the threshold is not taken.
     */
    uint64_t threshold;

    /** Type id bits. */
    int exitIdBits;

    /** Type id mask. */
    uint64_t exitIdMask;
};

#endif // __CPU_EDGE_LOCAL_EXIT_HH__
